public class InstagramSetup{
    public string userData {get;set;}
    public string existingMedia {get; set;}
    //Instagram_Conversation__c instaConvo {get;set;}
    
    //initialize

    
   public PageReference retreiveUserMedia() {  // Get Media      
    /*
    //get username Endpoint
        string url = 'https://api.instagram.com/v1/users/251993517/media/recent/?count=33&access_token=' + access_token;
    // Instantiate a new http object
        Http h = new Http();

    // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
    
    // Send the request, and return a response
        HttpResponse res = h.send(req);
        userData = res.getBody();   
        JSONMedia obj = JSONMedia.parse(userData);
        //System.assert(obj != null);
        system.debug('my obj =>' + obj);  
        system.debug('paginate =>' + obj.pagination);
        //system.debug('size =>' + obj.data.size());
        List<Instagram_Conversation__c> instaList = new List<Instagram_Conversation__c>();
        if(obj != null) {
            for(integer i=0; obj.data.size() > i; i++) {
                 Instagram_Conversation__c insta = new Instagram_Conversation__c();
                 insta.Recordtypeid = '012110000004KK2'; // Munch Post
                 insta.Published_Date__c = convertUnixDateTime(obj.data[i].created_time);  
                if(obj.data[i].caption != null){        
                    insta.Caption_Text__c = obj.data[i].caption.text;
                }
                insta.Media_ID__c = obj.data[i].id;
                insta.Instagram_Picture__c = obj.data[i].images.thumbnail.url;
                insta.Author_Full_Name__c =  obj.data[i].user.full_name;
                insta.Author_Username__c =  obj.data[i].user.username;
                insta.Profile_Picture__c = obj.data[i].user.profile_picture;
                insta.Instagram_ID__c = obj.data[i].user.id; 
                insta.Comment_Count__c = obj.data[i].comments.count;
                //string link = ;
                insta.Instagram_Link__c = obj.data[i].link;
                instaList.add(insta);
            }
        }
        
        
        try
        {
         Database.saveResult[] dsr = Database.insert(instaList, false);
         //insert instaList;
        } 
        catch(Exception e)
        {
            existingMedia = 'Media Already Exist!';
        }
         */
         //string res = globalInstagramPost.getInfoMediaService();
         globalInstagramPost.retreiveUserMediaInfo( );
        return null;
       
    }    

//demo
  public PageReference scheduleDemoSingleMediaComments() {

	  /* working Map<string, string> httpMediaMap = globalInstagramPost.createMediaCommentService();	
	  globalInstagramPost.postSchedCommentServiceRequest(httpMediaMap);	
	  */

	  	    Instagram_Token__c token = [SELECT Access_Token__c, User_Id__c FROM Instagram_Token__c LIMIT 1]; 
	        //hardcode values for POC Instagram button leads
	        String access_token = token.Access_Token__c;


			datetime dt = datetime.now();
            //String formatedDt = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');//format the datetime to make it Dynamic Soql ready
            List<Instagram_Conversation__c> instaList = [Select Id, Media_Id__c From Instagram_Conversation__c where Media_Id__c != null and (Next_API_Callout_Datetime__c = null OR Next_API_Callout_Datetime__c < : dt ) LIMIT 2];           

			for(Instagram_Conversation__c insta : instaList) {
				globalInstagramPost.postSchedCommentServiceRequest(insta.media_id__c, access_token); 
			    //make the call out  
			}

      return null;
    }




  public PageReference scheduleDemoSingleMediaLike() {


	  	    Instagram_Token__c token = [SELECT Access_Token__c, User_Id__c FROM Instagram_Token__c LIMIT 1]; 
	        //hardcode values for POC Instagram button leads
	        String access_token = token.Access_Token__c;


			datetime dt = datetime.now();
            //String formatedDt = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');//format the datetime to make it Dynamic Soql ready
            List<Instagram_Conversation__c> instaList = [Select Id, Media_Id__c From Instagram_Conversation__c where Media_Id__c != null and (Next_API_Callout_Datetime__c = null OR Next_API_Callout_Datetime__c < : dt ) LIMIT 2];           
			
			
			for(Instagram_Conversation__c insta : instaList) {
				globalInstagramPost.postSchedLikeServiceRequest(insta.media_id__c, access_token); 
			    //make the call out  
			}
			

      return null;
    }
    

}